package com.moore.controller;

import com.moore.bean.Community;
import com.moore.dto.ResponseData;
import com.moore.service.CommunityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/communities")
public class CommunityController {

    @Autowired
    private CommunityService communityService;

    @GetMapping("/get")
    public IPage<Community> getCommunities(
            @RequestParam int current,
            @RequestParam int size,
            @RequestParam Long candidateId) {
        Page<Community> page = new Page<>(current, size);
        return communityService.getCommunities(page, candidateId);
    }

    @GetMapping("/getByCommunityId")
    public Community getCommunityByCommunityId(@RequestParam Long communityId, @RequestParam Long candidateId) {
        return communityService.getCommunityByCommunityId(communityId,candidateId);
    }

    @PostMapping("/add")
    public ResponseData addCommunity(@RequestBody Community community) {
        int result = communityService.addCommunity(community);
        if (result > 0) return ResponseData.ok("发布成功");
        else return ResponseData.error("发布失败");
    }

    @GetMapping("/delete")
    public ResponseData deleteCommunity(@RequestParam Long communityId) {
        int result = communityService.deleteCommunity(communityId);
        if (result > 0) return ResponseData.ok("删除成功");
        else return ResponseData.error("删除失败");
    }

    @GetMapping("/report")
    public ResponseData reportCommunity(@RequestParam Long communityId) {
        int result = communityService.reportCommunity(communityId);
        if (result > 0) return ResponseData.ok("举报成功");
        else return ResponseData.error("举报失败");
    }
}